<?php

if (isset($_POST['submit']) ) {
include("../includes/dbInfo.php");
include("../includes/header.php");

?>

<meta http-equiv="refresh" content="2;url=http://www.hungmun.com/c410/site-gui/">
<link rel='stylesheet' type='text/css' href='layout.css' />
</head>
<body><div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">

                <div id="sidebar">
                    <?php
					// include the corresponding navigation page
                    include("../includes/navigation.php");
					?>     
                </div>

<?php
if ($_POST['parent'] == 'parent') {
$query = "INSERT INTO accounts VALUES ('', '".$_POST['birthday']."', '".$_POST['name']."', 'parent', SHA1('".$_POST['password']."'), '".$_POST['email']."','".$_POST['gender']."', '".$_POST['childComments']."')";
}

else {
$query = "INSERT INTO accounts VALUES ('', '".$_POST['birthday']."', '".$_POST['name']."', 'swimmer', SHA1('".$_POST['password']."'), '".$_POST['email']."','".$_POST['gender']."', '')";
}

mysql_query($query) or die("Cannot register user details, please contact Webmaster.". mysql_error());

$query = "SELECT userID FROM accounts WHERE email = '".$_POST['email']."'";
$results = mysql_query($query) or die("Cannot get userID for address entry, please contact Webmaster." . mysql_error() );
$row = mysql_fetch_array($results);

$query = "INSERT INTO address VALUES ('".$row['userID']."','".$_POST['address']."', '".$_POST['city']."', '".$_POST['postal']."')";
mysql_query($query) or die("Cannot register user address, please contact Webmaster." . mysql_error() );
 
?>
<div class="featured-post">
<div class="post-title">
<h2>Thank You For Registering!</h2>
</div></div></div>
<div class="clear"></div>
  <?php include("../includes/footer.php"); ?>
 </div> </div></body>
</html>
<?php 
}

else {
?>
<?php include("../includes/header.php") ?>

<!-- Found at http://www.tizag.com/javascriptT/javascriptform.php -->
<script type='text/javascript'>
function formValidator(){
	// Make quick references to our fields
	var name = document.getElementById('name');
	var addr = document.getElementById('address');
	var city = document.getElementById('city');
	var postal = document.getElementById('postal');
	var province = document.getElementById('province');
	var email = document.getElementById('email');
	var birthday = document.getElementById('birthday');
	var password = document.getElementById('password');
	var password2 = document.getElementById('password2');

	// Check each input in the order that it appears in the form!
	if(isAlphabet(name, "Please enter only letters for your name")){
		if(passValidator(password, password2, "Your passwords are not the same, please enter the same password in both fields")){
			if(birthdayValidator(birthday, "Please enter a valid birthday in the form YYYY-MM-DD")){
				if(notEmpty(addr, "Numbers and Letters Only for Address")){
					if(isAlphabet(city, "Please enter a valid City")){
						if(madeSelection(province, "Please Choose a Province")){
							if(isAltAlphaNumeric(postal, "Please enter a valid Postal Code")){
								if(emailValidator(email, "Please enter a valid email address")){
									return true;
								}
							}
						}
					}
				}
			}
		}
	}
	
	
	return false;
	
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}
function passValidator(elem, elem1, helperMsg){
	if(elem.value != elem1.value){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isAltAlphaNumeric(elem, helperMsg){
	var numericExpression = /^[a-zA-z]{1}[0-9]{1}[a-zA-z]{1}\s*[0-9]{1}[a-zA-z]{1}[0-9]{1}$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+\s*[a-zA-Z]*$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Select"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}


function birthdayValidator(elem, helperMsg){
	var birthdayExp = /^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/;
	if(elem.value.match(birthdayExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
</script>
<title>LazySwim - Register Account</title>
<link rel='stylesheet' type='text/css' href='layout.css' />
<script type='text/javascript' src='./login/ajax_login.js'></script>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js'></script>
<script type='text/javascript' src='./password_reset/ajax_password_reset.js'></script>
<style type="text/css">
<!--
.show {visibility:visible}
.hide {visibility:hidden}
-->
</style>
</head>

<body>
<div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">

                <div id="sidebar">
                    <?php
					// include the corresponding navigation page
                    include("../includes/navigation.php");
					?>     
                </div>

<!-- main body -->

                <div class="featured-post">
                    <div class="post-title">
                        <h2>Personal Information Form</h2>
                    </div>
                    <div class="post-entry">
					<p>Please fill out this form with your personal information.<br />
					(Fields marked with <font color = 'red'>*</font> must be entered.)</p>
					<h3>Registration Form</h3>

<form onsubmit='return formValidator()' action="register.php" method="post">
<table>
<tr><td>Name:</td><td> <input type='text' id='name' name='name' /><font color = 'red'>*</font></td></tr>
<tr><td>Password:</td><td> <input type='password' id='password' name='password' /><font color = 'red'>*</font></td></tr>
<tr><td>Confirm Password:</td><td> <input type='password' id='password2' name='password2' /><font color = 'red'>*</font></td></tr>
<tr><td>Birth Date (YYYY-MM-DD):</td><td><input type='text' id='birthday' name='birthday' /><font color = 'red'>*</font></td></tr>
<tr><td>Gender:</td><td>
		<select id="gender" name='gender'>
		<option value="Male">Male</option>
		<option value="Female">Female</option>
		</select>
		<font color = 'red'>*</font>
		</td></tr>
<tr><td>Street Address:</td><td> <input type='text' id='address' name='address' /><font color = 'red'>*</font></td></tr>
<tr><td>City:</td><td><input type='text' id='city' name='city' /><font color = 'red'>*</font></td></tr>
<tr><td>Province:</td><td>
		<select id="province" name='province'>
		<option value="AB">Alberta</option>
		<option value="BC">British Columbia</option>
		<option value="MB">Manitoba</option>
		<option value="NB">New Brunswick</option>
		<option value="NL">Newfoundland</option>
		<option value="NT">Northwest Territories</option>
		<option value="NS">Nova Scotia</option>
		<option value="NU">Nunavut</option>
		<option value="ON">Ontario</option>
		<option value="PE">Prince Edward Island</option>
		<option value="QC">Quebec</option>
		<option value="SK">Saskatchewan</option>
		<option value="YT">Yukon</option>
		</select>
		<font color = 'red'>*</font>
		</td></tr>
<tr><td>Postal code:</td><td><input type='text' id='postal' name='postal' /><font color = 'red'>*</font></td></tr>
<tr><td>E-mail Address:</td><td><input type='text' id='email' name='email' /><font color = 'red'>*</font></td></tr>
<tr><td>
<input type="checkbox" name="parent" id="parent" value="parent" onclick="document.getElementById('info').style.visibility = this.checked ? 'visible' : 'hidden'" /> I am a parent
</td></tr>
</table>
<div id="info" style="visibility:hidden">
Please enter your child(ren)'s full name(s) and birthday(s)<br />
<textarea name="childComments" cols="40" rows="4"></textarea></div>
<input type="submit" name="submit" value="Submit Your Entries" />
<input type="button" name="reset_form" value="Clear Your Entries" onclick="this.form.reset();" />

</form>


                    </div>
                </div>

             
                <div class="clear"></div>

            </div>
			<!-- Footer Goes Here -->
            <?php include("../includes/footer.php"); ?>
			<!-- Footer Ends Here -->
        </div>
    </div>
</div>
</body>
</html>


<?php } ?>